using System.Data;
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
using PoemGame.ApplicationService;
using PoemGame.ComputerAnswer.Db;
using PoemGame.ComputerAnswer.Shared;

using PoemGame.Domain.GameAggregate;
using PoemGame.Domain.PlayerAggregate;
using PoemGame.Domain.Services;
//using PoemGame.Repository.EF;
using PoemGame.WebDemoApplication;

using PoemGame.Domain.Services.Factory.WebApi;
using PoemGame.Repository.Simple;


var builder = WebApplication.CreateBuilder(args);

var services = builder.Services;

services.AddHttpClient();
services.AddScoped<IDomainServiceFactory<ICheckGameConditionService>, CheckGameConditionServiceFactory>();
services.AddScoped<IDomainServiceFactory<ICheckAnswerService>, CheckAnswerServiceFactory>();

//services.AddDbContext<PoemGameDbContext>(options => options.UseSqlServer(builder.Configuration.GetConnectionString("PoemGameConn")));

//services.AddDbContext<PoemGameDbContext>(options => options.UseSqlite(builder.Configuration.GetConnectionString("PoemGameConn")));

builder.Services.AddScoped<IDbConnection, SqliteConnection>(serviceProvider => {
    var conn = new SqliteConnection();
    conn.ConnectionString = builder.Configuration.GetConnectionString("SqlitePoemConn");
    return conn;
});

services
    .AddScoped<IGameFactory, GameFactory>()
    //.AddScoped<IGameRepository, GameRepository
    .AddSingleton<IGameRepository, GameRepository>()
    //.AddScoped<IPlayerRepository, PlayerRepository>()
    .AddSingleton<IPlayerRepository, PlayerRepository>()
    .AddScoped<IComputerAnswerFactory, ComputerAnswerFactory>()
    .AddScoped<IPoemGameWebDemoApplication, PoemGameWebDemoApplication>();

services.AddControllers();

services.AddCors(option => option.AddPolicy("cors",
    policy => policy.AllowAnyHeader()
        .AllowAnyMethod()
        .AllowAnyOrigin()));

builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();

if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseAuthorization();

app.MapControllers();
app.UseCors("cors");

app.Run();
